﻿namespace instamuz.Migrations
{
    using FluentMigrator;

    [Migration(0)]
    public class Migration0 : ForwardOnlyMigration
    {
        public override void Up()
        {
            Execute.Sql(@"
    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_FOLLOWERS_ACCOUNT]') AND parent_object_id = OBJECT_ID('FOLLOWERS'))
alter table FOLLOWERS  drop constraint FK_FOLLOWERS_ACCOUNT

GO


    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_FOLLOWERS_ACCOUNT]') AND parent_object_id = OBJECT_ID('FOLLOWERS'))
alter table FOLLOWERS  drop constraint FK_FOLLOWERS_ACCOUNT

GO


    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_FOLLOWING_ACCOUNT]') AND parent_object_id = OBJECT_ID('FOLLOWING'))
alter table FOLLOWING  drop constraint FK_FOLLOWING_ACCOUNT

GO


    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_FOLLOWING_ACCOUNT]') AND parent_object_id = OBJECT_ID('FOLLOWING'))
alter table FOLLOWING  drop constraint FK_FOLLOWING_ACCOUNT

GO


    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_POST_ACCOUNT]') AND parent_object_id = OBJECT_ID('POST'))
alter table POST  drop constraint FK_POST_ACCOUNT

GO


    if exists (select * from dbo.sysobjects where id = object_id(N'ACCOUNT') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table ACCOUNT
GO


    if exists (select * from dbo.sysobjects where id = object_id(N'FOLLOWERS') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table FOLLOWERS
GO


    if exists (select * from dbo.sysobjects where id = object_id(N'FOLLOWING') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table FOLLOWING
GO


    if exists (select * from dbo.sysobjects where id = object_id(N'POST') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table POST
GO


    create table ACCOUNT (
        ACCOUNT_ID INT IDENTITY NOT NULL,
       LOGIN NVARCHAR(MAX) not null,
       EMAIL NVARCHAR(MAX) not null,
       PASSWORD_HASH NVARCHAR(MAX) not null,
       PASSWORD_SALT NVARCHAR(MAX) not null,
       primary key (ACCOUNT_ID)
    )
GO


    create table FOLLOWERS (
        ACCOUNT_ID INT not null,
       FOLLOWER_ID INT not null,
       primary key (ACCOUNT_ID, FOLLOWER_ID)
    )
GO


    create table FOLLOWING (
        ACCOUNT_ID INT not null,
       FOLLOWING_ID INT not null,
       primary key (ACCOUNT_ID, FOLLOWING_ID)
    )
GO


    create table POST (
        POST_ID INT IDENTITY NOT NULL,
       DESCRIPTION NVARCHAR(MAX) null,
       CREATED_AT DATETIME null,
       ACCOUNT_ID INT not null,
       primary key (POST_ID)
    )
GO


    alter table FOLLOWERS 
        add constraint FK_FOLLOWERS_FOLLOWER 
        foreign key (FOLLOWER_ID) 
        references ACCOUNT
GO


    alter table FOLLOWERS 
        add constraint FK_FOLLOWERS_ACCOUNT 
        foreign key (ACCOUNT_ID) 
        references ACCOUNT
GO


    alter table FOLLOWING 
        add constraint FK_FOLLOWING_FOLLOWING 
        foreign key (FOLLOWING_ID) 
        references ACCOUNT
GO


    alter table FOLLOWING 
        add constraint FK_FOLLOWING_ACCOUNT 
        foreign key (ACCOUNT_ID) 
        references ACCOUNT
GO


    alter table POST 
        add constraint FK_POST_ACCOUNT 
        foreign key (ACCOUNT_ID) 
        references ACCOUNT
GO");
        }
    }
}